import {
    getHotelListApi,getHotelAllListApi,getSightAllListApi,getSightListApi,getFoodListApi,getFoodAllListApi,getHaiListApi,getHaiAllListApi
} from "../../api/spot";
export default {
    namespaced: true,
    state: {
        // 是否在加载请求数据
        isLoad: false,

        // 发现页当前的activeIndex
        activeIndex: 0,

        //酒店列表的数据
        hotelList: [],
        //景点列表的数据
        sightList: [],
        //美食列表的数据
        foodList: [],
        //出去嗨列表的数据
        haiList: [],


        // 第几次请求数据
        num: 1,
    },
    mutations: {
        // 设置是否正在加载详情
        setIsLoad(state, value) {
            state.isLoad = value;
        },

        // 设置酒店的数据列表
        setHotelList(state, value) {
            const num = state.num;
            // console.log(num);
            if(num == 1){
                state.hotelList = value;
                this.commit("spot/setNum", num+1);
                return
            }
            
            state.hotelList.push(...value)
            // console.log(value);
        },
        // 设置酒店ALL数据列表
        setHotelAllList(state, value) {
            state.hotelList = value;
        },

        // -----------------------------
        // 设置景点ALL数据列表
        setSightAllList(state, value) {
            state.sightList = value;
        },
        // 设置景点的数据
        setSightList(state, value) {
            const num = state.num;
            // console.log(num);
            if(num == 1){
                state.sightList = value;
                this.commit("spot/setNum", num+1);

                return
            }
            
            state.sightList.push(...value)
            // console.log(value);
        },
        // ==============================
        // 设置美食ALL数据列表
        setFoodAllList(state, value) {
            state.foodList = value;
        },
        // 设置美食数据列表
        setFoodList(state, value) {
            const num = state.num;
            // console.log(num);
            if(num == 1){
                state.foodList = value;
                this.commit("spot/setNum", num+1);

                return
            }
            
            state.foodList.push(...value)
            // console.log(value);
        },
        // ==============================
        // 设置出去嗨ALL数据列表
        setHaiAllList(state, value) {
            state.haiList = value;
        },
        // 设置出去嗨的数据
        setHaiList(state, value) {
            const num = state.num;
            // console.log(num);
            if(num == 1){
                state.haiList = value;
                this.commit("spot/setNum", num+1);

                return
            }
            
            state.haiList.push(...value)
            // console.log(value);
        },

        // --------------=--------------

        // 设置当前的导航索引
        setActiveIndex(state, value) {
            state.activeIndex = value;
        },
        // 设置第几次请求数据
        setNum(state, value) {
            state.num = value
        },

        // 添加点赞数的列表
        setlikeygoodNumadd(state,addIndex){
            if(state.activeIndex === 0){

            }else if(state.activeIndex === 1){
                // console.log("likeygoodNum++");
                state.hotelList[addIndex].likeygoodNum++;
            }else if(state.activeIndex === 2){
                // console.log("likeygoodNum++");

                state.sightList[addIndex].likeygoodNum++;
            }else if(state.activeIndex === 3){
                // console.log("likeygoodNum++");

                state.foodList[addIndex].likeygoodNum++;
            }else if(state.activeIndex === 4){
                // console.log("likeygoodNum++");

                state.haiList[addIndex].likeygoodNum++;
            }
        },

        // 减少点赞数
        setlikeygoodNumdel(state,addIndex){
            // console.log(addIndex);
            if(state.activeIndex === 0){

            }else if(state.activeIndex === 1){
                state.hotelList[addIndex].likeygoodNum--;
            }else if(state.activeIndex === 2){
                // console.log(addIndex);
                state.sightList[addIndex].likeygoodNum--;
            }else if(state.activeIndex === 3){
                state.foodList[addIndex].likeygoodNum--;
            }else if(state.activeIndex === 4){
                state.haiList[addIndex].likeygoodNum--;
            }
        }
        

    },

    actions: {
        // 修改activeIndex
         changeActiveIndex(context, index) {
            context.commit("setActiveIndex", index);
            context.commit("setNum", 1);
            context.dispatch("requestData",index);    
             
        },

        
        // 下拉加载请求
        morehotelList(context){
            const index =  context.state.activeIndex;
            context.dispatch("requestData", index)
            const num =  context.state.num;
            context.commit("setNum", num+1);

        },

        //请求列表的数据
        async requestData(context, index) {
            // 设置请求加载数据
            context.commit("setIsLoad", true);
            const num =  context.state.num;
            // console.log(num);
            //请求酒店列表的数据
            if(index === 0){

            }
            else if (index === 1) {
                const {
                    data
                } = await getHotelListApi({
                    num
                });

            context.commit("setHotelList", data.list);

            }else if (index === 2) {
                // 请求景点得数据
                const {
                    data
                } = await getSightListApi({
                    num
                });

            context.commit("setSightList", data.list);

            }else if (index === 3) {
                // 请求美食的数据
                const {
                    data
                } = await getFoodListApi({
                    num
                });

            context.commit("setFoodList", data.list);

            }else{
                // 请求美食的数据
                const {
                    data
                } = await getHaiListApi({
                    num
                });

            context.commit("setHaiList", data.list);

            }
            // 设置不请求加载数据
            context.commit("setIsLoad", false);
            // context.commit("setHotelListNum", num++);
        },

        // 请求当前的num全部数据列表
        async requestALLData(context,index){
            context.commit("setIsLoad", true);
            const num =  context.state.num;
            // console.log(num);

            if(index === 0){
                // const {
                //     data
                // } = await getHotelAllListApi({
                //     num
                // });
            }else if (index === 1) {
                console.log(1);
                //请求酒店列表的数据
                const {
                    data
                } = await getHotelAllListApi({
                    num
                });

            context.commit("setHotelAllList", data.list);

            }else if (index === 2) {
                console.log(2);
                // 请求景点得数据
                const {
                    data
                } = await getSightAllListApi({
                    num
                });

            context.commit("setSightAllList", data.list);

            }else if (index === 3) {
                // 请求美食的数据
                const {
                    data
                } = await getFoodAllListApi({
                    num
                });

            context.commit("setFoodAllList", data.list);

            }else{
                // 请求美食的数据
                const {
                    data
                } = await getHaiAllListApi({
                    num
                });

            context.commit("setHaiAllList", data.list);

            }


            // 设置不请求加载数据
            context.commit("setIsLoad", false);
        }


    },


};